package org.smarthings.things.automation.scripting.engine;

import java.util.HashMap;
import java.util.Map;




import org.smarthings.things.automation.Rule;

import com.espertech.esper.client.EventBean;

public class ScriptingContexBuilder {

	private BasicScriptingContext context;

	private ScriptingContexBuilder() {
		this.context = new BasicScriptingContext();
	}

	public static ScriptingContexBuilder create() {
		return new ScriptingContexBuilder();
	}

	public ScriptingContexBuilder rule(Rule rule) {
		this.context.setRule(rule);
		return this;
	}

	public ScriptingContexBuilder events(EventBean[] events) {
		this.context.setEvents(events);
		return this;
	}

	public ScriptingContexBuilder bindings(Map<String, Object> bindings) {
		this.context.setBindings(bindings);
		return this;
	}

	public ScriptingContexBuilder binding(String key, Object value) {
		this.context.setBinding(key, value);
		return this;
	}

	public ScriptingContext build() {
		return this.context;
	}

	private class BasicScriptingContext implements ScriptingContext {

		private Rule rule;
		private Map<String, Object> bindings = new HashMap<String, Object>(5);
		private EventBean[] events; // FIXME check how we can get a more friendly way to obtain the
									// original events!

		public Rule getRule() {
			return rule;
		}

		public EventBean[] getEvents() {
			return events;
		}

		public Map<String, Object> getBindings() {
			return bindings;
		}

		private void setRule(Rule rule) {
			this.rule = rule;
		}

		private void setEvents(EventBean[] events) {
			this.events = events;
		}

		private void setBindings(Map<String, Object> bindings) {
			this.bindings = bindings;
		}

		private void setBinding(String key, Object value) {
			this.bindings.put(key, value);
		}

	}

}
